
    /**  
    * @Title: FtpUtil.java
    * @Package com.utils.file
    * @Description: 
    * @author likai
    * @date 2016年9月1日
    * @version V1.0  
    */
    
package com.utils.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.web.multipart.MultipartFile;

import com.supers.exception.ExceptionUtils;
import com.utils.properties.PropertyUtil;


/**
    * @ClassName: FtpUtil
    * @Description: ftp文件处理
    * @author likai
    * @date 2016年9月1日
    *
    */

public class FtpUtil {
	

	/* 
	    * @Description:ftp上传文件
	    * 
	    * @param:fis文件流，filename上传文件名称，address文件上传路径,propPath配置文件路径
	    *
	    */
	public static boolean addfile(Object file,String propPath,String filename,String picPath) {
		Properties prop =PropertyUtil.getProp(propPath);
		FTPClient ftp=new FTPClient(); 
		InputStream fis=null;
		try {	
			if (file instanceof File) {
				fis=new FileInputStream((File)file);
			}
			if (file instanceof MultipartFile) {
				fis=((MultipartFile)file).getInputStream();
			}
			if (connect(ftp,picPath,prop.getProperty("ip"), Integer.parseInt(prop.getProperty("port")), prop.getProperty("username"), prop.getProperty("psw"))) {				
				ftp.enterLocalPassiveMode();
				ftp.storeFile(filename,fis);    
				return true;
			}  
			return false;
		} catch (Exception e) {
			System.err.println("文件上传出错啦");
			System.err.println(ExceptionUtils.getExceptionMessage(e));
			return false;
		}finally {
			if (ftp!=null && ftp.isConnected()) {
				try {
					ftp.logout();
					ftp.disconnect();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fis!=null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	/* 
	    * @Description:ftp上传文件
	    * 
	    * @param:fis文件流，filename上传文件名称，address文件上传路径,propPath配置文件路径
	    *
	    */
	public static boolean addfileByIo(InputStream fis,String propPath,String filename,String address) {
		Properties prop =PropertyUtil.getProp(propPath);
		FTPClient ftp=new FTPClient(); 
		try {				
			if (connect(ftp,prop.getProperty(address),prop.getProperty("ip"), Integer.parseInt(prop.getProperty("port")), prop.getProperty("username"), prop.getProperty("psw"))) {				
				ftp.enterLocalPassiveMode();
				ftp.storeFile(filename,fis);    
				return true;
			}  
			return false;
		} catch (Exception e) {
			System.err.println("文件上传出错啦");
			System.err.println(ExceptionUtils.getExceptionMessage(e));
			return false;
		}finally {
			if (ftp!=null && ftp.isConnected()) {
				try {
					ftp.logout();
					ftp.disconnect();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fis!=null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	/* 
	    * @Description:ftp删除文件
	    * 
	    * @param:，filename要删除的文件名称，address文件所在目录,propPath配置文件路径
	    *
	    */
	public static  boolean deletefile(String propPath,String filename,String address) {
		Properties prop =PropertyUtil.getProp(propPath);
		FTPClient ftp=new FTPClient(); 
		try {	
			if (connect(ftp,prop.getProperty(address),prop.getProperty("ip"), Integer.parseInt(prop.getProperty("port")), prop.getProperty("username"), prop.getProperty("psw"))) {				
				ftp.deleteFile(filename); 
				return true;
			}  
			return false;
		} catch (Exception e) {
			return false;
		}finally {
			if (ftp!=null && ftp.isConnected()) {
				try {
					ftp.logout();
					ftp.disconnect();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	
	
	/* 
	    * @Description:ftp文件下载
	    * 
	    * @param:，filename要删除的文件名称，address文件所在目录,propPath配置文件路径
	    *
	    */
    /*public static FTPFile   getFile(String propPath,String filename,String address) { 
    	Properties prop =PropertyUtil.getProp(propPath);
		FTPClient ftp=new FTPClient(); 
        try {
			if (connect(ftp,prop.getProperty(address),prop.getProperty("ip"), Integer.parseInt(prop.getProperty("port")), prop.getProperty("username"), prop.getProperty("psw"))) {
				String[] filenames=ftp.listNames();
				System.out.println(filenames.length);
				for (int i = 0; i < filenames.length; i++) {
					System.out.println(filenames[i]);
				}
				FTPFile file=ftp.mlistFile(filename);
			   return file;
			} else { 
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
    }*/
	
    /* 
	    * @Description:链接ftp
	    * 
	    * 
	    *
	    */
	private static boolean connect(FTPClient ftp,String path,String addr,int port,String username,String password) throws Exception {    

	     int reply;    
	     ftp.connect(addr,port);    
	     ftp.login(username,password);  
	     ftp.setBufferSize(1024);
	     ftp.setControlEncoding("utf-8");
	     ftp.setFileType(FTPClient.BINARY_FILE_TYPE);    
	     reply = ftp.getReplyCode();    
	     
	     if (!FTPReply.isPositiveCompletion(reply)) {    
	         ftp.disconnect();    
	         return false;    
	     } 
	     if (isDirExist(ftp, path)) {			
	    	 ftp.changeWorkingDirectory(path);    
	    	 return true;
		 }
	     return false;   
	}   
	
	/**
     * 检查文件夹是否存在
     * 
     * @param dir
     * @param ftpClient
     * @return
     */
    private static Boolean isDirExist(FTPClient ftp,String dir) {
        try {
        	ftp.changeWorkingDirectory(dir.replace("\\", "/"));
        } catch (Exception e) {
            return false;
        }
        return true;
    }
	
	/*public static void main(String[] args) {
		System.out.println(deletefile("/ftp.properties", "0a8328cd371f40eaa779746ef7b3add3.jpg", "noticeaddress"));
		
	}*/
	
}
